﻿using System;
using System.Linq;
using Plugins;
using StockAccessors;
using StockAnalyst2.Plugins.Engine;
using StockAnalyst2.StockGrid;
using StockEngine.RealTime;

namespace StockAnalyst2
{
	internal class ApplicationCloser
	{
		#region Attributes

		private readonly IStockDataAccessor _accessor;
		private readonly PluginSystem _pluginSystem;
		private readonly AcquisitionSystem _acquisitionSystem;

		#endregion

		#region Properties

		public bool ShutdownDone { get; private set; }

		#endregion

		#region Public Methods

		public ApplicationCloser(IStockDataAccessor accessor, PluginSystem pluginSystem, AcquisitionSystem acquisitionSystem)
		{
			_accessor = accessor;
			_pluginSystem = pluginSystem;
			_acquisitionSystem = acquisitionSystem;
		}

		public void Shutdown()
		{
			_acquisitionSystem.Dispose();
			_accessor.Dispose();

			foreach (IDisposable plugin in _pluginSystem.Plugins.OfType<IDisposable>())
			{
				plugin.Dispose();
			}

			this.ShutdownDone = true;
			System.Windows.Application.Current.Shutdown();
		}

		#endregion
	}
}
